﻿using Microsoft.AspNetCore.Mvc;
using GGGK.Film.Models;
using GGGK.Film.Models.Dto;
using GGGK.Film.IService;
using AutoMapper;
using GGGK.Film.Comm;
using Microsoft.EntityFrameworkCore;
using GGGK.Film.Models.Models;

namespace GGGK.Film.Api.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class UsersController:ControllerBase
    {
        private readonly IUserInfoService svc;
        private readonly IMapper mapper;

        public UsersController(IUserInfoService svc,IMapper mapper)
        {
            this.svc = svc;
            this.mapper = mapper;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public PagedResult<UserInfoDto> Get(string? userName,int pageIndex=1,int pageSize=10)
        {
            var data = svc.Query(o => true);

            if(!string.IsNullOrEmpty(userName)) data = svc.Query(o => o.UserName.Contains(userName));

            int total = data.Count();
            data = data.OrderByDescending(o => o.CreateTime)
                       .Skip(pageSize * (pageIndex - 1)).Take(pageSize).Include(o=>o.Base_LoginInfos);

            var dto = mapper.Map<IEnumerable<UserInfoDto>>(data);

            return new PagedResult<UserInfoDto> { code = 1, msg = "成功", total=total, data=dto };
        }

        /// <summary>
        /// 根据id获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public Result<UserInfoDto> Get(int id)
        {
            //var data = svc.Query(o => o.Id==id).Include(o=>o.Base_LoginInfos).FirstOrDefault();
            var data = svc.Get(o=>o.Id == id);

            var dto = mapper.Map<UserInfoDto>(data);

            return new Result<UserInfoDto> { code = 1,msg="成功",data=dto};
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]
        public Result Post(UserPostDto param)
        {
            var userInfo = mapper.Map<Base_UserInfo>(param);
            var loginInfo = mapper.Map<Base_LoginInfo>(param);

            userInfo.Base_LoginInfos.Add(loginInfo);

            if (svc.Add(userInfo))
                return new Result() { code = 1, msg = "成功"};
            else
                return new Result() { code = 1, msg = "失败" };
            
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPut]
        public Result Put(int id,UserPutDto param)
        {
            var user = svc.Get(o=>o.Id == id);

            if (user == null)
                return new Result(){code= 1,msg="数据不存在"};

            user.UserName = param.UserName;
            user.Sex = param.Sex;
            user.Tel = param.Tel;
            user.Email = param.Email;
            user.IdCard = param.IdCard;
            user.Status = param.Status?? user.Status;
            user.DeptId = param.DeptId;

            if (svc.Update(user))
                return new Result() { code = 1, msg = "成功" };
            else
                return new Result() { code = 1, msg = "失败" };
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public Result Delete(int id)
        {
            var user = svc.Query(o=>o.Id == id).Include(o=>o.Base_LoginInfos).FirstOrDefault();

            if (user == null)
                return new Result() { code = 1, msg = "数据不存在" };

            user.Status = 0;


            if (user.Base_LoginInfos.FirstOrDefault() != null) user.Base_LoginInfos.First().Status = 0;

            svc.Update(user);

            return new Result() { code = 1, msg = "删除成功" };
        }
    }
}
